<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link href="../res/css/weui.css" rel="stylesheet" type="text/css" />
    <link href="../res/css/weuix.css" rel="stylesheet" type="text/css" />
    <link href="../res/css/mp_common.css" rel="stylesheet" type="text/css" />
    <script src="../res/js/zepto.min.js" type="text/javascript"></script>
    <script src="../res/js/zepto.weui.js" type="text/javascript"></script>
    <script src="../res/js/php.js" type="text/javascript"></script>
    <script src="../res/js/picker.city.js" type="text/javascript"></script>
    <script src="../res/js/mp_common.js?v=1" type="text/javascript"></script>
    <style>        
        .weui-input{ font-size:14px;}
        label{ font-size:14px;}     
        input
        {
            text-align:left;
        }  
        .lable_div
        {
            height:20px;
            border:1px solid #b7b2b2;
            border-radius:5px;
            text-align:center;
            line-height:20px;
            font-size:10px;
            margin:5px;
        }
        table td
        {
            border:none;
        }     
        body{margin: 0px;}
    </style>
</head>
<body ontouchstart >
<div class="weui-cells weui-cells_form" style="margin:0px;">
    <div class="weui-flex" style="padding:6px 0; background-color:#07c160;">
        <div class="weui-flex__item">
            <div class="placeholder">
                <span class="icon icon-109" style="color:#FFF;" onclick="actPageGo(-1)"></span>
            </div>
        </div>
        <div class="weui-flex__item"><div class="placeholder" style="text-align:center;color:#FFF;">添加收货地址</div></div>
        <div class="weui-flex__item"><div class="placeholder" style="text-align:right;color:#FFF;"></div></div>
    </div>
    <div class="weui-cell" style="margin-top:20px;">
        <div class="weui-cell__hd">
            <label class="weui-label" style="width:70px;">收货人</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="txt_name" placeholder="请输入收货人姓名" type="text">
        </div>
    </div>
    <div class="weui-cell" >
        <div class="weui-cell__hd">
            <label class="weui-label" style="width:70px;">手机号码</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="txt_tel" placeholder="请输入收货人手机号码" type="tel">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd" style="width:70px;"><label class="weui-label">所在地区</label>
        </div>
        <div class="weui-cells weui-cells_form" id="region" style="margin-top:0px; width: 100%;">


            <div class="weui-cell" style="padding:0px;">
                <div class="weui-cell__bd">
                 
                    <input class="weui-input" id="txt_city" type="text" value="广东省 广州市 荔湾区">
                </div>
            </div>
        </div>
        <!--<input class="weui-input" id="txt_szdq" placeholder="请输入省（市）、市（区）、县（区）" value="" type="text">-->
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd" style="width:70px;"><label class="weui-label">详细地址</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="txt_xxdz" placeholder="如：道路、门牌号、小区、楼栋号、单元室等" value="" type="text">
        </div>
    </div> 
    <div class="weui-cell">
        <div class="weui-cell__hd" style="width:70px;"><label class="weui-label">地址标签</label></div>
        <div class="weui-cell__bd">
            <div>
                <table id="lable_div">
                    <!-- <tr>
                        <td onclick="save_lable(this)" data-lable_name="l"><div class="lable_div">家</div></td>
                        <td onclick="save_lable(this)" data-lable_name="2"><div class="lable_div">公 司</div></td>
                        <td onclick="save_lable(this)" data-lable_name="3"><div class="lable_div">学 校</div></td>
                    </tr>
                    <tr>
                        <td onclick="add_lable()"><div class="lable_div">+</div></td>
                        <td></td>
                        <td></td>
                    </tr> -->
                </table>
            </div>
        </div>
    </div>   
    <div class="weui-cell weui-cell_switch">
        <div class="weui-cell__bd"><label class="weui-label">设为默认地址</label></div>
        <div class="weui-cell__ft">
            <label for="switchCP" class="weui-switch-cp">
                <input id="switchCP" class="weui-switch-cp__input" type="checkbox">
                <div class="weui-switch-cp__box"></div>
            </label>
        </div>
    </div> 
    <div class="page-bd-15" style="margin-top:20px;">
        <a href="javascript:;" class="weui-btn weui-btn_primary" onclick="address_num()">保&nbsp;&nbsp;存</a>
    </div>
    <br />
    <br />
    <!-- <div class="weui-flex" style="padding:5px 10px 0 10px; background-color:#05C1A9;"></div> -->
</div>


<div id="dialog-alert" style="display: none;">
    <div class="weui-mask"></div>
    <div class="weui-dialog">
        <div class="weui-dialog__hd"><strong class="weui-dialog__title">新增标签</strong></div>
         <div class="weui-cell" >
            <div class="weui-cell__hd">
                 <label class="weui-label" style="width:70px;">标签</label>
            </div>
            <div class="weui-cell__bd">
                <input class="weui-input" id="txt_lable" placeholder="请输入标签" type="tel">
            </div>
        </div>
        <div class="weui-dialog__ft">
            <a href="javascript:;" id="cacel-dialog" style="cursor: pointer" class="weui-dialog__btn weui-dialog__btn_default">取消</a>
            <a href="javascript:;" id="pass-dialog" style="cursor: pointer" class="weui-dialog__btn weui-dialog__btn_primary">确定</a>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var lable_name = "";
    var str_name = "";
    var str_tel = "";
    var str_city = "";
    var str_xxdz = "";
    var str_default = "";
    var user_id = getQst("user_id").trim();
    if (isSpace(user_id)) {
        user_id = "1892";  //用户id
    }
    strOrgId = getQst("orgid");
	strOpenId = getQst("openid");
    RXnum = getQst("prescription_id").trim();  //处方号
    $(function () {
        // checkUSBD("user_address_add.htm");
        lable_list();
        
	$('#region').on('click',function(){
		$('#txt_name').blur();
		$('#txt_tel').blur();
		$('#txt_xxdz').blur();
	});
    });


    function lable_list() {
        GetPageData('标签查询', { "actName": "actPatient", "actTag": "Find_lable", "user_id": user_id }, function (rstData) {
            var lable_div = "";
            if (rstData.total > 0) {
                var tr_num = rstData.total % 3 == 0 ? parseFloat(rstData.total / 3) + 1 : parseFloat(rstData.total / 3);
                for (var i = 0; i < tr_num; i++) {
                    lable_div += '<tr>';
                    for (var j = 0; j < 3; j++) {
                        if (i * 3 + j == (rstData.total)) {
                            lable_div += '<td onclick="add_lable()"><div class="lable_div">+</div></td>';
                        }
                        else if (i * 3 + j > (rstData.total)) {
                            lable_div += '<td></td>';
                        }
                        else if (i * 3 + j < (rstData.total)) {
                            lable_div += '<td onclick="save_lable(this)" data-lable_name="' + rstData.rows[i * 3 + j].lable_name + '"><div class="lable_div">' + rstData.rows[i * 3 + j].lable_name + '</div></td>';
                        }
                    }
                    lable_div += '</tr>';
                }
                $("#lable_div").html(lable_div);
            }
        });
    }

    ///选择收货地址
    $("#txt_city").cityPicker({
        title: "请选择收货地址",
        onChange: function (picker, values, displayValues) {

        }
    });

    //选择标签
    function save_lable(obj) {
        lable_name = $(obj).attr("data-lable_name");  //地址标签
        $(".lable_div").css("background-color", "#fff");
        $(".lable_div").css("color", "#000");
        $(".lable_div").css("border", "1px solid #b7b2b2");
        $(obj).children("div").css("color", "#fff");
        $(obj).children("div").css("border", "none");
        $(obj).children("div").css("background-color", "#07c160");
    }


    ///新增标签
    function add_lable() {
        // $('#dialog-alert').show();
        $.prompt({
            title: '添加标签',
            text: '',
            input: '请输入标签名',
            empty: false, // 是否允许为空
            onOK: function (input) {
                var lable_name = input;
                if (!isSpace(lable_name)) {
                    GetPageData('修改默认地址', { "actName": "actPatient", "actTag": "Address_lable", "user_id": user_id, "lable_name": lable_name }, function (rstData) {
                        if (rstData.total > 0) {
                            if (rstData.rows[0].msg == "成功") {
                                lable_list();
                                showJSZ("标签添加成功");
                            }
                            else if (rstData.rows[0].msg == "失败") {
                                showJSZ("标签添加失败");
                            }

                        }
                        else if (rstData.total == -1) {
                            showJSZ("程序错误，请联系管理员");
                        }

                    });
                }

            },
            onCancel: function () {
                //点击取消
            }
        });
    }

    //查询地址数量
    function address_num() {
        // debugger;
        str_name = $("#txt_name").val();  //收货人
        str_tel = $("#txt_tel").val();   //电话
        str_city = $("#txt_city").val();  //城市地址
        str_xxdz = $("#txt_xxdz").val();  //详细地址
        str_default = $("#switchCP").is(':checked');   //是否默认

        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if (isSpace(str_name)) {
            showJSZ("请填写收货人姓名");
            return;
        }
        if (isSpace(str_tel)) {
            showJSZ("请填写收货人号码");
            return;
        }
        if (!myreg.test(str_tel)) {
            showJSZ("请填写正确的手机号码");
            return;
        }
        if (isSpace(str_city)) {
            showJSZ("请填写收货地址");
            return;
        }
        if (isSpace(str_xxdz)) {
            showJSZ("请填写收货详细地址");
            return;
        }
        GetPageData('地址数量查询', { "actName": "actPatient", "actTag": "Query_address_num", "user_id": user_id }, function (rstData) {
            if (rstData.total > 0) {
                if (rstData.rows[0].num < 20) {
                    Address_mr();
                }
                else if (rstData.rows[0].num >= 20) {
                    showJSZ("地址已满");
                }

            }
            else if (rstData.total == -1) {
                showJSZ("程序错误，请联系管理员");
            }
        });
    }


    //修改默认地址
    function Address_mr() {
        if (str_default) {
            GetPageData('修改默认地址', { "actName": "actPatient", "actTag": "Address_mr", "user_id": user_id }, function (rstData) {
                if (rstData.total > 0) {
                    if (rstData.rows[0].msg == "成功") {
                        save_address();
                    }
                    else if (rstData.rows[0].msg == "失败") {
                        showJSZ("设置默认地址失败");
                    }

                }
                else if (rstData.total == -1) {
                    showJSZ("程序错误，请联系管理员");
                }

            });
        }
        else {
            save_address();
        }
    }


    //新增地址
    function save_address() {
        GetPageData('药店选择', { "actName": "actPatient", "actTag": "Save_address", "user_id": user_id, "str_name": str_name, "str_tel": str_tel, "str_city": str_city, "str_xxdz": str_xxdz, "lable_name": lable_name, "str_default": str_default }, function (rstData) {
            
            if (rstData.total > 0) {
                if (rstData.rows[0].msg == "成功") {
                    showJSZ("保存成功");
                    // actPageGo(-1)
					//2020.04.07-------
					// history.go(-1)
					//-----------------
                    actToPage('user_dtl1.htm?orgid=' + strOrgId + '&openid=' + strOpenId + '&prescription_id=' + RXnum + '&user_id='+ user_id);

                }
                else {
                    showJSZ("保存失败");
                }
            }
            else if(rstData.total == -1){
                showJSZ("程序错误，请联系管理员");
            }
        });
    }


    //返回列表
    function ToPage() {
        /* actToPage('user_list.htm?user_id=' + user_id); */
		history.back(-1)
    }
</script>
